package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.TrainingUserDetails;
import com.battle.pojo.param.trainingUserDetails.*;
import com.battle.pojo.vo.trainingUserDetails.*;
import com.battle.pojo.dto.trainingUserDetails.*;
import com.battle.service.repository.in.TrainingUserDetailsRepository;
import com.battle.service.service.in.TrainingUserDetailsService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：我的训练-项目记录服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TrainingUserDetailsServiceImpl implements TrainingUserDetailsService {

    private final TrainingUserDetailsRepository trainingUserDetailsRepository;

    public TrainingUserDetailsServiceImpl(TrainingUserDetailsRepository trainingUserDetailsRepository) {
        this.trainingUserDetailsRepository = trainingUserDetailsRepository;
    }


    /**
     * 创建我的训练-项目记录
     *
     * @param param
     */
    @Override
    public void createTrainingUserDetails(CreateTrainingUserDetailsParam param) {
        TrainingUserDetails trainingUserDetails = new TrainingUserDetails();
        BeanUtils.copyProperties(param, trainingUserDetails);
        Long id = IdGenerator.SNOW.generate();
        trainingUserDetails.setId(id);
        trainingUserDetailsRepository.create(trainingUserDetails);
    }

    /**
     * 修改我的训练-项目记录
     *
     * @param param
     */
    @Override
    public void editTrainingUserDetails(EditTrainingUserDetailsParam param) {
        Long id = param.getId();
        TrainingUserDetails trainingUserDetails = trainingUserDetailsRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, trainingUserDetails);
        trainingUserDetailsRepository.edit(trainingUserDetails);
    }

    /**
     * 移除我的训练-项目记录
     *
     * @param id
     */
    @Override
    public void removeTrainingUserDetails(Long id) {
        trainingUserDetailsRepository.removeById(id);
    }

    /**
     * 启动/禁用我的训练-项目记录
     *
     * @param id
     */
    @Override
    public void enabledTrainingUserDetails(Long id) {
        TrainingUserDetails trainingUserDetails = trainingUserDetailsRepository.getByIdCheck(id);
        trainingUserDetails.setIsDisable(!trainingUserDetails.getIsDisable());
        trainingUserDetailsRepository.edit(trainingUserDetails);
    }

    /**
     * 获取我的训练-项目记录详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTrainingUserDetailsVo getTrainingUserDetails(Long id) {
        TrainingUserDetails trainingUserDetails = trainingUserDetailsRepository.getByIdCheck(id);
        GetTrainingUserDetailsVo vo = new GetTrainingUserDetailsVo();
        BeanUtils.copyProperties(trainingUserDetails, vo);
        return vo;
    }

    /**
     * 获取我的训练-项目记录 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTrainingUserDetailsListVo> getTrainingUserDetailsList(GetTrainingUserDetailsListParam param) {

        SelectTrainingUserDetailsDto dto = new SelectTrainingUserDetailsDto();
        BeanUtils.copyProperties(param, dto);
        Page<TrainingUserDetails> page = trainingUserDetailsRepository.selectTrainingUserDetails(dto);
        List<TrainingUserDetails> trainingUserDetailsList = page.getRecords();
        List<GetTrainingUserDetailsListVo> list = new ArrayList<>();
        for (TrainingUserDetails trainingUserDetails : trainingUserDetailsList) {
            GetTrainingUserDetailsListVo vo = new GetTrainingUserDetailsListVo();
            BeanUtils.copyProperties(trainingUserDetails, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取我的训练-项目记录下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTrainingUserDetailsDownListVo> getTrainingUserDetailsDownList(GetTrainingUserDetailsDownListParam param) {
        TrainingUserDetails po = new TrainingUserDetails();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TrainingUserDetails> trainingUserDetailsList = trainingUserDetailsRepository.getListByEntity(po);

        List<GetTrainingUserDetailsDownListVo> list = new ArrayList<>();
        for (TrainingUserDetails trainingUserDetails : trainingUserDetailsList) {
            GetTrainingUserDetailsDownListVo vo = new GetTrainingUserDetailsDownListVo();
            BeanUtils.copyProperties(trainingUserDetails, vo);
            list.add(vo);
        }
        return list;
    }


}



